import { defineAsyncComponent } from 'vue';
import globalMixin from './mixins/globalMixin';
import { customApiExecute, getCallableCompList, getJsonCallableCompList } from './utils/common-methods';
import { useAnvilCodeStore } from './store/modules/code';
import { useUserStore } from '@/store/modules/user';
import './assets/iconfont/iconfont.css';
// 引入avue
import '@smallwei/avue/lib/index.css';
import Avue from '@smallwei/avue';
import './styles/index.scss'
// import './styles/anvil-code.scss'

const importAll = (r) => {
  let cache = {};
  for (let k in r) {
    const arr = k.split('/');
    const name = arr[arr.length - 1].replace('.vue', '');
    cache[name] = r[k];
    // 涉及到业务组件页面功能
    cache[name].fileName = name
  }
  return cache;
};

// 创建Context,获取低代码组件文件列表
const allFile = importAll(import.meta.glob('./components/**/*.vue'));
// 获取项目自定义组件文件列表
const customFile = importAll(import.meta.glob('/src/components/Code/components/**/*.vue'));

export { useAnvilCode } from './hooks/code/useAnvilCode';

export const anvilCodeComponents = allFile

export default {
  install(Vue) {
    const files = Object.assign({}, allFile, customFile)
    for (let key in files) {
      Vue.component(key, defineAsyncComponent(files[key]));
      // 为了能够在子表单中使用自定义组件
      Vue.component('avue-' + key, defineAsyncComponent(files[key]));
    }
    Vue.config.globalProperties.getCallableCompList = getCallableCompList;
    Vue.config.globalProperties.getJsonCallableCompList = getJsonCallableCompList;
    Vue.config.globalProperties.useAnvilCodeStore = useAnvilCodeStore;
    Vue.config.globalProperties.useUserStore = useUserStore;

    Vue.mixin(globalMixin);
    Vue.use(Avue);
  }
};
